<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix card-header">
        <span>类别列表</span>
        <el-button type="primary" @click="addDialog = true">添加分类</el-button>
      </div>
      <el-table stripe border style="width: 100%" :data="list">
        <el-table-column prop="id" label="ID" width="150"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="obj">
            <el-button size="mini" type="primary" @click="amend(obj.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="del(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加的对话框 -->
    <el-dialog :before-close="cancel" title="添加分类" :visible.sync="addDialog" width="500px">
      <el-form label-width="80px" :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改分类的对话框 -->
    <el-dialog title="修改类别" :visible.sync="amendDialog" width="35%">
      <el-form label-width="80px" :model="amendData" :rules="rules" ref="amendForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="amendData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="amendData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="amendDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryAPI, addCategoryAPI, deleteCategoryAPI, updateCategoryAPI } from '@/api/category'
export default {
  data() {
    return {
      // 控制添加的数据项
      list: [
        { id: 1, cate_name: '音乐', cate_alias: 'music' },
        { id: 2, cate_name: '电影', cate_alias: 'movie' },
        { id: 3, cate_name: '动漫', cate_alias: 'cartoon' }
      ],
      addDialog: false, // 表示隐藏对话框,
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      amendDialog: false,
      // 控制修改的数据项
      amendData: {},
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '只能是1~15位的字母数字组合', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCategory()
  },
  methods: {
    // 获取
    async getCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.list = res.data
        console.log(this.list)
      }
    },
    // 添加
    addCategory() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message) // 提示
          this.$refs.addForm.resetFields()
          this.addDialog = false
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel() {
      this.$refs.addForm.resetFields() // 重置表单
      this.addDialog = false // 关闭弹框
    },
    // 删除
    del(id) {
      this.$confirm('确定要删除吗?', '提示', {
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            const { data: res } = await deleteCategoryAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getCategory()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 数据回填
    amend(row) {
      this.amendDialog = true
      this.amendData = { ...row }
    },
    // 修改分类
    updateCategory() {
      this.$refs.amendForm.validate(async valid => {
        if (valid) {
          const { data: res } = await updateCategoryAPI(this.amendData)
          if (res.code === 0) {
            this.$message.success(res.message)
            this.getCategory()
            this.amendDialog = false
          } else {
            this.$message.error(res.message)
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  font-size: 13px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
